//
//  RegisterViewController.m
//  YYZB
//
//  Created by 顾悦 on 16/3/28.
//
//

#import "RegisterViewController.h"

#import "ThemeColor.h"
#import "TextFieldBGView.h"

#import "NSString+MobileNo.h"

@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UIButton *termsButton;

@property (weak, nonatomic) IBOutlet TextFieldBGView *usernameBGView;
@property (weak, nonatomic) IBOutlet TextFieldBGView *passwordBGView;
@property (weak, nonatomic) IBOutlet TextFieldBGView *verifyCodeBGView;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ThemeColor lightGrayBGColor];
    self.title = @"注册";
    self.verifyCodeButton.enabled = NO;
    self.submitButton.enabled = NO;
    self.termsButton.selected = YES;
    self.usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.verifyCodeTextField.keyboardType = self.usernameTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordTextField.secureTextEntry = YES;
    [self.usernameTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.verifyCodeTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)termsCheckButtonTapped:(UIButton *)button {
    button.selected = !button.selected;
    [self updateSubmitButtonStatus];
}

- (BOOL)shouldFetchVerifyCode {
    return [self.usernameTextField.text isMobileNumber];
}

- (void)updateVerifyCodeButtonStatus {
    if ([self shouldFetchVerifyCode]) {
        self.verifyCodeButton.enabled = YES;
    } else {
        self.verifyCodeButton.enabled = NO;
    }
}

- (BOOL)shouldSubmit {
    if ([self.usernameTextField.text isMobileNumber]
        && self.verifyCodeTextField.text.length > 0
        && self.passwordTextField.text.length > 0
        && self.termsButton.selected) {
        return YES;
    }
    return NO;
}

- (void)updateSubmitButtonStatus {
    if ([self shouldSubmit]) {
        self.submitButton.enabled = YES;
    } else {
        self.submitButton.enabled = NO;
    }
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (textField == self.usernameTextField) {
        [self updateVerifyCodeButtonStatus];
    }
    [self updateSubmitButtonStatus];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
